package managedbean.staff;

import exception.ExistException;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.StaffAccountSessionBean;
import session.stateless.SystemMessagesBean;
//import session.stateless.StaffAccountSessionBean;
import util.entityhelper.StaffAccountState;
import util.enumhelper.SupportedLocale;
import util.enumeration.Language;
import util.generator.StaffMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class StaffChangeInitialPasswordManagedBean implements Serializable {
    @EJB
    private StaffAccountSessionBean staffAccountMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    // fields passed from session
    private Long staffAccountId;
    private String preferredLocale;
    
    // other fields from database to be displayed
    private String username;
    private String currentPassword;
    
    
    private String newPassword;
    private String confirmPassword;
    private Date passwordChangeDate;
    private MenuModel staffMenuModel;
    private PageRedirectorBean redirect;
    
    
    public StaffChangeInitialPasswordManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            // get session values
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            // put back session values
            ctx.getExternalContext().getSessionMap().put("staffAccountId", staffAccountId);
        }
        else {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goStaffLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */      
    }
    
    public void initView(PhaseEvent event) {
        try {
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);
            // get data from backend, exception alrd thrown by backend if these methods fail.
            
            staffAcctState = staffAccountMgr.getStaffAccountByID(staffAccountId);
            
            username = staffAcctState.getUsername();
            
            Language savedLocale = SupportedLocale.getLanguageBySupportedLocaleString(staffAcctState.getStaff().getLocale());
            
            /* PRE-SET MENU BASED ON ACCESS RIGHTS */
            StaffMenuGenerator menuGenerator = new StaffMenuGenerator(staffAcctState);
            staffMenuModel = menuGenerator.generateStaffMenu();
            /* END PRE-SET MENU BASED ON ACCESS RIGHTS */ 
        }
        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }
    

    public void changeInitialPassword(ActionEvent event) {
        try {
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);

            Long id = staffAcctState.getId();

            staffAccountMgr.changeInitialPassword(id, newPassword);
        }
        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }

        String statusMessage = systemMessages.getSystemMessage("PASSWORD_CHANGE_SUCCESS", preferredLocale);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
        
        reset();
    }
    
    private void reset() {
        newPassword = "";
        confirmPassword = "";
        
    }
    
    public StaffAccountSessionBean getStaffAccountMgr() {
        return staffAccountMgr;
    }

    public void setStaffAccountMgr(StaffAccountSessionBean staffAccountMgr) {
        this.staffAccountMgr = staffAccountMgr;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public Long getStaffAccountId() {
        return staffAccountId;
    }

    public void setStaffAccountId(Long staffAccountId) {
        this.staffAccountId = staffAccountId;
    }

    public StaffAccountState getStaffAcctState() {
        return staffAcctState;
    }

    public void setStaffAcctState(StaffAccountState staffAcctState) {
        this.staffAcctState = staffAcctState;
    }

    public String getCurrentPassword() {
        return currentPassword;
    }

    public void setCurrentPassword(String currentPassword) {
        this.currentPassword = currentPassword;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public Date getPasswordChangeDate() {
        return passwordChangeDate;
    }

    public void setPasswordChangeDate(Date passwordChangeDate) {
        this.passwordChangeDate = passwordChangeDate;
    }

    public MenuModel getStaffMenuModel() {
        return staffMenuModel;
    }

    public void setStaffMenuModel(MenuModel staffMenuModel) {
        this.staffMenuModel = staffMenuModel;
    }
    
}
